using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using ECommerce.Library.Web;
using System.Collections.Generic;
using ECommerce.Library;
using System.IO;
using Entity;
using Ext.Net;
using eShop.Model;
using eShop.Business;

public partial class admin_product : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;

        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
            LoadProductCategory();
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindProducts(e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }

    private void BindProducts(string sort, int start, int limit)
    {
        ReadWriteCustomCollection<Product> data = null;
        int totalCount = 0;
        if (String.IsNullOrEmpty(sort))
            sort = "ProductId";

        if (this._useSearch)
        {
           
            string _searchProductName = txtToolbarSearch.Text.Trim();
            string _itemNumber = txtToolbarSearchItemNumber.Text.Trim();
            int _parentCategoryId;
          
            Int32.TryParse(ddlToolbarCategories.SelectedItem.Value, out _parentCategoryId);
            if (_parentCategoryId == SiteUtility.SuperRootCategoryId)
                _parentCategoryId = 0;
            //_parentCategoryId = SiteUtility.SuperRootCategoryId;

            data = Product.GetBackEndProductListBySearchAdmin(_searchProductName,_itemNumber, _parentCategoryId, false, sort, start, limit);
            totalCount = Product.GetBackEndProductListBySearchCountAdmin(_searchProductName, _itemNumber ,_parentCategoryId, false);

            //Session["_useSearch"] = null;
            
        }
        else
        {
            data = Product.GetBackEndProductListBySearch("",0, false, sort, start, limit);
            totalCount = Product.GetBackEndProductListBySearchCount("",0, false);
        }

        Store1.DataSource = data;
        Store1.DataBind();

        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    protected void LoadProductCategory()
    {
        //ddlParent.Items.Clear();
        ReadWriteCustomCollection<KeyValuePair<string, string>> productCategoryTree = CategoryManager.GetSubCategoryTree(SiteUtility.SuperRootCategoryId);
        foreach (KeyValuePair<string, string> productCategory in productCategoryTree)
        {
            //ddlParent.Items.Add(new System.Web.UI.WebControls.ListItem(productCategory.Key, productCategory.Value));
            ddlToolbarCategories.Items.Add(new Ext.Net.ListItem(Server.HtmlDecode(productCategory.Key), productCategory.Value));
        }
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void UpdateProduct(int id, string productName, double price, string displayOrder, bool isActive)
    {
        Product product = Product.Load(id);
        product.ProductName = productName;
        product.ProductActualPrice = price;
        product.DisplayOrder = displayOrder;
        product.IsActive = isActive;
        product.Update();
        //GridPanel1.Store.Primary.DataBind();
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void DeleteProducts(string ids)
    {
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray) idList.Add(Convert.ToInt32(id));
        Product.DeleteProducts(idList);
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void SearchProduct()
    {
        _useSearch = true;
        PagingToolbar1.SetPageIndex(1);
        Store1.DataBind();
        
        //BindProducts("ProductId", 0, 50);
    }

}
